#pragma once
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <fstream>
#include "tool.hpp"
#include<mutex>
namespace myindex
{
    struct DocInfo
    {
        std::string title;
        std::string content;
        std::string url;
        int id;
    };
    struct InvertedElem
    {
        int id;
        std::string word;
        int weight;
    };
    // 倒排拉链
    typedef std::vector<InvertedElem> InvertedList;
    class Index
    {
    public:
        Index() {}
        ~Index() {}
        static std::mutex _mutex;
        DocInfo *Getindex(int id)
        {
            if (id >= forward_index.size())
            {
                std::cerr << "id range error" << std::endl;
            }
            return &forward_index[id];
        }
        InvertedList *Getlist(const std::string &word)
        {
            auto it = inverted_index.find(word);
            if (it == inverted_index.end())
            {
                std::cerr << "no list" << std::endl;
            }
            return &(it->second);
        }
        // 根据去标签，格式化之后的文档，构建正排和倒排索引
        // data/raw.html/raw.txt
        bool Buildindex(const std::string &input)
        {
            std::ifstream in(input, std::ios::in | std::ios::binary);
            if (!in.is_open())
            {
                std::cerr << "fail" << input << std::endl;
                return false;
            }
            std::string line;
            int cout=0;
            while (std::getline(in, line))
            {
                DocInfo *doc = BulidIndex(line);
                if (doc == nullptr)
                {
                    std::cerr << "build fail" << std::endl;
                    continue;
                }
                BulidInvertedIndex(*doc);
                cout++;
                std::cout<<"已建立文档:"<<cout<<std::endl;
            }
            std::cout<<"文档建立完成"<<std::endl;
            return true;
        }

    private:
        DocInfo *BulidIndex(const std::string &line)
        {
            // 1.字符串切分
            std::string sep = "\3";
            std::vector<std::string> result;
            tool::StringTool::CutString(line, result, sep);
            // 2.把切分结果填充到DocInfo中
            DocInfo doc;
            doc.title = result[0];
            doc.content = result[1];
            doc.url = result[2];
            doc.id = forward_index.size();
            // 3.插入到正排的vector中
            forward_index.push_back(std::move(doc)); // 会发生拷贝
            return &forward_index.back();
        }
        bool BulidInvertedIndex(const DocInfo &doc) {
            struct word_cnt
            {
                int title_cnt;
                int content_cnt;
                word_cnt() : title_cnt(0), content_cnt(0) {}
            };
            std::unordered_map<std::string, word_cnt> word_map; // 用来暂存词频的映射表
    
            // 对标题进行分词
            std::vector<std::string> title_words;
            tool::JiebaUtil::CutString(doc.title, &title_words);
    
            // 对标题进行词频统计
            for (std::string s : title_words)
            {
                boost::to_lower(s);      // 需要统一转化成为小写
                word_map[s].title_cnt++; // 如果存在就获取，如果不存在就新建
            }
    
            // 对文档内容进行分词
            std::vector<std::string> content_words;
            tool::JiebaUtil::CutString(doc.content, &content_words);
    
            // 对内容进行词频统计
            for (std::string s : content_words)
            {
                boost::to_lower(s);
                word_map[s].content_cnt++;
            }
            #define X 10
            #define Y 1
            for (auto &word : word_map)
            {
                InvertedElem item;
                item.id = doc.id;
                item.word = word.first;
                item.weight = X * word.second.title_cnt + Y * word.second.content_cnt; // 相关性
                InvertedList &inverted_list = inverted_index[word.first];
                inverted_list.push_back(std::move(item));
            }
            return true;
        }

    private:
        // 正牌索引
        std::vector<DocInfo> forward_index;
        // 倒排索引,关键字和倒排拉链的映射关系
        std::unordered_map<std::string, InvertedList> inverted_index;
    };
}